<template>
	<view class="content">
		<image class="logo" :src="headerUrl"></image>
		<view class="text-area">
			<text class="title">{{nickName}}</text>
		</view>
		<view class="text-area button-style" @click="login()">
			登录
		</view>
		<view class="text-area button-style" @click="show">展示</view>
		<view @click="show()">{{openid}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickName: 'Hello',//昵称
				headerUrl:'../../static/logo.png',//头像
			}
		},
		onLoad() {
			this.login()
		},
		methods: {
			show(){
				console.log(this.openid)
			},
			login(){
				var that = this
				uni.showModal({
					mask: true,
					title: '温馨提示',
					content: '授权微信登录后才能正常使用小程序功能',
					success(res) {
						if (res.confirm) {
							uni.getUserProfile({
								desc: '获取你的昵称、头像',
								success: userRes => {
									if (userRes.errMsg == 'getUserProfile:ok' && userRes.userInfo != undefined) {
										var userInfo = {
											avatarUrl: userRes.userInfo.avatarUrl,
											nickName: userRes.userInfo.nickName
										}
										//对页面中的变量进行赋值
										that.nickName = userRes.userInfo.nickName
										that.headerUrl = userRes.userInfo.avatarUrl
										that.getUserOpenId(userInfo)
										
									} else {
										uni.showToast({
											icon:"none",
											title:"获取失败，请重试"
										})
									}
								},
								fail: error => {}
								
							});
						} else if (res.cancel) {}
					}
				});
			},
			getUserOpenId(userInfo){
				var that = this
				var useropenid=''
				var code=code
				uni.login({
					provider: 'weixin',
					success(loginAuth) {
						code=loginAuth.code
						var data = {'code':code}
						var path = 'http://42.193.152.244/login'
						uni.request({
							url:path,
							data:data,
							success(res){
								userInfo['openid'] = res
								useropenid=res['data']['openid']
								console.log('request的openid:'+useropenid)
								try {
									uni.setStorageSync('openid', useropenid);
								} catch (e) {
									console.log(e)
								}
								;
								// uni.setStorageSync()({
								// 	key: 'openid',
								// 	data: useropenid,
								// 	success: function () {
								// 		console.log("存储成功")
								// 	},
								// 	fail(res) {
								// 		console.log(res)
								// 		console.log(useropenid)
								// 	}
								// });
							}
						})
					}
				})
			}
			

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.button-style{
		width: 150rpx;
		height: 60rpx;
		background-color: #007AFF;
		border-radius: 10rpx;
		margin-top: 60rpx;
	}
</style>
